#include "widget.h"
#include "ui_widget.h"

#include <QTimer>

Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget),
    timer(new QTimer(this)),
    flag(false)
{
    ui->setupUi(this);
    connect(timer,&QTimer::timeout,this,&Widget::handle);
    ui->pgb->setValue(0);
    ui->pgb->setAlignment(Qt::AlignCenter);
    ui->pbt->setText("start");
}

Widget::~Widget()
{
    delete ui;
}

void Widget::handle()
{
    int value = ui->pgb->value()+1;
    if(value > 100)
    {
        timer->stop();
        ui->pbt->setText("restart");
        flag = false;
        ui->pgb->setStyleSheet("QProgressBar::chunk {background-color: #00FF00;}"); //完成时为绿色
        return;
    }

    ui->pgb->setValue(value);
}

void Widget::on_pbt_clicked()
{
    if(ui->pgb->value() >= 100)
    {
        ui->pgb->setValue(0);
        ui->pbt->setText("start");
        return;
    }

    //true -> false
    if(flag)
    {
        timer->stop();
        ui->pgb->setStyleSheet("QProgressBar::chunk {background-color: #FFFF00;}"); //停止为黄色
        ui->pbt->setText("start");
        flag = false;
    }
    //false -> true
    else
    {
        timer->start(100);
        ui->pgb->setStyleSheet("QProgressBar::chunk {background-color: #FF0000;}"); //加载时是红色
        ui->pbt->setText("pause");
        flag = true;
    }
}
